﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Xml;

namespace STAR.Spring.Framework.Config
{
    public class ConfigSectionHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, System.Xml.XmlNode section)
        {
            ConfigInfo configInfo = new ConfigInfo();
            try
            {
                XmlNode resourceNode = section;
                if (resourceNode != null)
                {
                    string configPath = resourceNode.Attributes["uri"].Value;
                    XmlDocument doc = new XmlDocument();
                    doc.Load(System.Web.HttpContext.Current.Server.MapPath(configPath));
                    XmlNodeList nodeList = doc.SelectNodes("Root/CustomMethods/CustomMethod");
                    foreach (XmlNode item in nodeList)
                    {
                        XmlAttribute nameAttr = item.Attributes["Name"];
                        XmlAttribute typeAttr = item.Attributes["Type"];
                        if (nameAttr != null && typeAttr != null)
                        {
                            configInfo.CustomResponseInfos.Add(item.Attributes["Name"].Value, item.Attributes["Type"].Value);
                        }
                    }
                    configInfo.IsAnaysleOk = true;
                }
                else
                {
                    throw new Exception("没有配置resource结点");
                }
            }
            catch (Exception ex)
            {
                configInfo.IsAnaysleOk = false;
                configInfo.AnaysleException = ex;
            }

            return configInfo;
        }
    }
}
